<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Requests\UserAccountRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Hash;
use DB;
use App\user_detail;
class SettingUserController extends Controller
{
      /**
       * 个人发表微博的列表页
       * @return [type] [description]
       */
      public function getMywb()
      {

          $userid = session('id');

          //李明坤
          $res = DB::table('posts')
              -> join('user_detail','posts.uid','=','user_detail.id')
              -> join('types','posts.tid','=','types.tid')
              -> where('recycle','=','1')
              -> where('delete','=','1')
              -> where('caogao','=','1')
              -> where('uid',$userid)
              -> orderBy('subTime','desc')
              -> get();

          $types = DB::table('types')
              -> orderBy(\DB::raw('RAND()'))
              -> take(10)
              -> get();

          //苑卫强
          $nickName = DB::table('user_detail')->where('user_detail.id',$userid)->first();

          $prov = DB::table('address')->where('address.uid',$userid)->first();

          return view('homes.MyWB',['nickName'=>$nickName,'prov'=>$prov, 'weibos' => $res, 'types'=>$types]);
      }

        /**
         * 个人详情页
         * @return [type] [description]
         */
      public function getSetting()
      {
          return view('homes.setting');
      }

      /**
       * 显示个人详细信息
       * @return [type] [description]
       */
      public function getAccount()
      {
        return view('homes.account');
      }
      /**
       * 执行个人详细信息
       * @param  Request $request [description]
       * @return [type]           [description]
       */
      public function postDoaccount(UserAccountRequest $request)
      {

          $nickName = $request->nickName;

          // $age = $request->age;
          $sex = $request->sex;
          $star= $request->star;
          $birth= $request->birth;
          $email= $request->email;
          $prov = $request->prov;
          $city = $request->city;
          $sign = $request->sign;
          $street = $request->street;
          $id = session('id');
          
          //获取数据内的用户地址
         $res = DB::table('address')->where('uid',$id)->first();

         //判断数据是否为空,为空执行插入，不为空执行修改
         if(empty($res)){
          //判断地区是否为空，为空不插入，不为空插入 
            if(empty($street)){
                 DB::table('address')->insert(['city'=>$city,'prov'=>$prov,'uid'=>$id]);
                 
               }else{
                   DB::table('address')
                     ->insert(['prov'=>$prov,'city'=>$city,'street'=>$street,'uid'=>$id]);
                     
                   }
           }else{
               DB::table('address')
                    ->where('uid',$id)
                    ->update(['prov'=>$prov,'city'=>$city,'street'=>$street]);
                    
           }    
          //修改用户的个人信息
          $res_2 = DB::table('user_detail')
                 ->where('id',$id)
                 ->update(['email'=>$email,'nickName'=>$nickName,'sex'=>$sex,'star'=>$star,'birth'=>$birth,'sign'=>$sign]);    
            //判断个人信息设置的提示信息
              if($res_2){
                return back()->with('info','设置成功');
              }else{
                return back()->with('info','设置失败');
              }   
       
      }


      
      //手机绑定
       public function getMobile()
      {
        return view('homes.mobile');
      }
      /**
       * 模板
       * @return [type] [description]
       */
      public function getDesign()
      {
        return view('homes.design');
      }

      /**
       * 修改密码显示页面
       */
      public function getRepassword()
      {
        return view('homes.repassword');
      }
      /**
       * 执行密码的修改
       */
      public function postRepassword(Request $request)
      {
          $this->validate($request, [
              
                'repassword' => 'required|regex:/^\S{6,20}$/|same:rtpassword',
                
            ],[
                
                'repassword.required' => '密码必填',
                'repassword.regex' => '密码格式不正确',
                'repassword.same' => '两次密码不一致'
               
            ]);

          //用户的id
         $id = session('id');
         $user = User::where('id',$id)->first();
      
         // $password =  $user -> password = Hash::make($request->input('password'));
         if(Hash::check($request->password, $user->password)){
                $password =  $user -> password = Hash::make($request->input('repassword'));
                $success = DB::table('users')
                        ->where('id', $id)
                        ->update(['password' => $password]);

                if($success){
                    return back()->with('info','设置成功');
                }else{
                    return back()->with('info','设置失败');
                }    
                      
                    
         }else{
              return back()->with('info','当前密码和旧密码不一致');
         }    
          
          
      }

     
    /**
     * 头像修改
     * 
     */
    public function getTouxiang()
    {   
        $id = session('id');
        $user = DB::table('user_detail')->where('id',$id)->first();
        return view('homes.avatar',['user_detail'=>$user]);
    }
    public function postDotouxiang(Request $request)
    {
        if (!empty($request -> img)) {
            $user = new User_detail;
            if($request->hasFile('img')) {
                //创建文件的名字
                $filename = time().rand(10000,99999);
                //获取文件的后缀
                $suffix = $request->file('img')->getClientOriginalExtension();
                //文件夹
                $dirname = './uploads/profile/';
                //文件名
                $file = $filename .'.'. $suffix;

                //移动
                $request->file('img')->move($dirname,$file);
                $dirname = trim($dirname,'.');
                //修改图片属性
                $tu = $user->img = $dirname.$file;
            } 
         }else {
            return redirect() -> back();
        }
        $id = session('id');
        $res = DB::table('user_detail')->where('id',$id)->update(['profile'=>$tu]);
        if($res) {
            return redirect('/setting/touxiang');
        } else {
            return back();
        }
        
            
    }

}
